//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.apache.dubbo.metadata.store.redis;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.metadata.identifier.MetadataIdentifier;
import org.apache.dubbo.metadata.identifier.MetadataIdentifier.KeyTypeEnum;
import org.apache.dubbo.metadata.support.AbstractMetadataReport;
import org.apache.dubbo.rpc.RpcException;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * dubbo+redis配置元数据中心时无法指定redis的数据库，所以将元数据中心配置类拿出来手动修改下，放在相同的路径下
 */
public class RedisMetadataReport extends AbstractMetadataReport {
    private static final Logger logger = LoggerFactory.getLogger(RedisMetadataReport.class);
    JedisPool pool;
    Set<HostAndPort> jedisClusterNodes;
    private int timeout;
    private String password;

    public RedisMetadataReport(URL url) {
        super(url);
        this.timeout = url.getParameter("timeout", 1000);
        this.password = url.getPassword();
        if (url.getParameter("cluster", false)) {
            this.jedisClusterNodes = new HashSet();
            List<URL> urls = url.getBackupUrls();
            Iterator var3 = urls.iterator();

            while(var3.hasNext()) {
                URL tmpUrl = (URL)var3.next();
                this.jedisClusterNodes.add(new HostAndPort(tmpUrl.getHost(), tmpUrl.getPort()));
            }
        } else {
            this.pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), this.timeout, StringUtils.isEmpty(url.getPassword()) ? null : url.getPassword(), url.getParameter("db.index", 0));
//            this.pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), this.timeout, url.getPassword());
        }

    }

    protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) {
        this.storeMetadata(providerMetadataIdentifier, serviceDefinitions);
    }

    protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String value) {
        this.storeMetadata(consumerMetadataIdentifier, value);
    }

    private void storeMetadata(MetadataIdentifier metadataIdentifier, String v) {
        if (this.pool != null) {
            this.storeMetadataStandalone(metadataIdentifier, v);
        } else {
            this.storeMetadataInCluster(metadataIdentifier, v);
        }

    }

    private void storeMetadataInCluster(MetadataIdentifier metadataIdentifier, String v) {
        try {
            JedisCluster jedisCluster = new JedisCluster(this.jedisClusterNodes, this.timeout, this.timeout, 2, this.password, new GenericObjectPoolConfig());
            Throwable var4 = null;

            try {
                jedisCluster.set(metadataIdentifier.getIdentifierKey() + ".metaData", v);
            } catch (Throwable var14) {
                var4 = var14;
                throw var14;
            } finally {
                if (jedisCluster != null) {
                    if (var4 != null) {
                        try {
                            jedisCluster.close();
                        } catch (Throwable var13) {
                            var4.addSuppressed(var13);
                        }
                    } else {
                        jedisCluster.close();
                    }
                }

            }

        } catch (Throwable var16) {
            logger.error("Failed to put " + metadataIdentifier + " to redis cluster " + v + ", cause: " + var16.getMessage(), var16);
            throw new RpcException("Failed to put " + metadataIdentifier + " to redis cluster " + v + ", cause: " + var16.getMessage(), var16);
        }
    }

    private void storeMetadataStandalone(MetadataIdentifier metadataIdentifier, String v) {
        try {
            Jedis jedis = this.pool.getResource();
            Throwable var4 = null;

            try {
                jedis.set(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), v);
            } catch (Throwable var14) {
                var4 = var14;
                throw var14;
            } finally {
                if (jedis != null) {
                    if (var4 != null) {
                        try {
                            jedis.close();
                        } catch (Throwable var13) {
                            var4.addSuppressed(var13);
                        }
                    } else {
                        jedis.close();
                    }
                }

            }

        } catch (Throwable var16) {
            logger.error("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + var16.getMessage(), var16);
            throw new RpcException("Failed to put " + metadataIdentifier + " to redis " + v + ", cause: " + var16.getMessage(), var16);
        }
    }
}
